package com.it;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class _6_3ServletConfigTest extends HttpServlet {
    //（1）、声明ServletConfig属性
    private ServletConfig config;
    public _6_3ServletConfigTest() {
        super();
    }

    @Override
    public void destroy() {
        super.destroy();
    }
    //(2)、设置带参数的初始化函数init
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init();
        //(3)将传递过来的配置文件对象，赋值给声明ServletConfig属性
        this.config=config;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //（4）、使用ServletConfig的getInitParameter方法获取初始化参数
        String charset = this.config.getInitParameter("charset");
        //设置响应字符集格式为UTF-8
        response.setCharacterEncoding(charset);
        response.setContentType("text/html");
        //返回一个PrintWriter对象，Servlet使用它来输出字符串形式的正文数据
        PrintWriter out = response.getWriter();
        //以下为输出的HHTML正文数据
        out.println("<HTML>");
        out.println("<HEAD><TITLE>ServletConfig</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("获取InitParamServlet的初始化参数\"encoding\"的字符串值："+charset);
        out.println("</BODY>");
        out.println("</HTML>");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
